package org.example.src.controller;

import org.example.src.dto.request.TeacherProfileRequest;
import org.example.src.dto.response.ApiResponse;
import org.example.src.dto.response.TeacherProfileResponse;
import org.example.src.entity.Teacher;
import org.example.src.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/teachers")
@CrossOrigin(origins = "*")
public class TeacherController extends BaseController {

    @Autowired
    private TeacherService teacherService;

    @GetMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<List<Teacher>>> getAllTeachers() {
        List<Teacher> teachers = teacherService.findAllTeachers();
        return success(teachers);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT') or (hasRole('TEACHER') and #id == authentication.principal.id)")
    public ResponseEntity<ApiResponse<Teacher>> getTeacherById(@PathVariable Long id) {
        Teacher teacher = teacherService.findTeacherById(id);
        if (teacher == null) {
            return notFound("教师未找到");
        }
        return success(teacher);
    }

    @GetMapping("/teacher-id/{teacherId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<Teacher>> getTeacherByTeacherId(@PathVariable String teacherId) {
        Teacher teacher = teacherService.findTeacherByTeacherId(teacherId);
        if (teacher == null) {
            return notFound("教师未找到");
        }
        return success(teacher);
    }

    @PostMapping
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Teacher>> createTeacher(@Valid @RequestBody Teacher teacher) {
        Teacher createdTeacher = teacherService.createTeacher(teacher);
        return created(createdTeacher);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN') or (hasRole('TEACHER') and #id == authentication.principal.id)")
    public ResponseEntity<ApiResponse<Teacher>> updateTeacher(@PathVariable Long id, @Valid @RequestBody Teacher teacher) {
        Teacher updatedTeacher = teacherService.updateTeacher(id, teacher);
        if (updatedTeacher == null) {
            return notFound("教师未找到");
        }
        return success("教师信息更新成功", updatedTeacher);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ApiResponse<Void>> deleteTeacher(@PathVariable Long id) {
        teacherService.deleteTeacher(id);
        return noContent();
    }

    @PutMapping("/{id}/profile")
    @PreAuthorize("hasAnyRole('ADMIN') or (hasRole('TEACHER') and #id == authentication.principal.id)")
    public ResponseEntity<ApiResponse<TeacherProfileResponse>> updateTeacherProfile(@PathVariable Long id, @Valid @RequestBody TeacherProfileRequest request) {
        try {
            TeacherProfileResponse response = teacherService.updateTeacherProfile(id, request);
            return success("教师档案更新成功", response);
        } catch (Exception e) {
            return error("更新失败: " + e.getMessage());
        }
    }
}
